//
//  ZJFindPswViewController.m
//  ZiJinLian
//
//  Created by lzy on 2017/3/9.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ZJFindPswViewController.h"
#import "ZJFindPswSecondViewController.h"
#import "ZJphone.h"

@interface ZJFindPswViewController ()<UITextFieldDelegate>
@property(assign, nonatomic) NSInteger timeCount;
@property(strong, nonatomic) NSTimer *timer;
@property(nonatomic,copy)NSString *code;

@end

@implementation ZJFindPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    [self initTitle:MyString(@"找回密码")];
    self.FindPswView=[[ZJFindPswView alloc]init];
    self.FindPswView.frame=self.view.frame;
    [self.view addSubview:self.FindPswView];
    //键盘 手势
    self.view.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    [self.view addGestureRecognizer:singleTap];
    
    [self BtnTarget];
    [self setTFdeleget];
    //文本框输入状态监测
    [self.FindPswView.phoneTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void) textFieldDidChange:(id) sender {
    UITextField *_field = (UITextField *)sender;
    NSString *str=_field.text;
    if (str) {
        [self.FindPswView.nextBtn setBackgroundColor:button_Red_Color];
        [self.FindPswView.nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}
-(void)BtnTarget
{
    [_FindPswView.nextBtn addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
    [_FindPswView.getsecuritycodeBtn addTarget:self action:@selector(SendBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)setTFdeleget
{
    _FindPswView.phoneTF.delegate=self;
    _FindPswView.securitycodeTF.delegate=self;
}
#pragma -------------------获取验证码 按钮 方法————————————————————————
-(void)SendBtnClick:(UIButton *)btn
{
    
    if(self.FindPswView.phoneTF.text.length==0|self.FindPswView.phoneTF.text.length<11){
          [self showMessage:MyString(@"请输入正确的手机号码") ];
        return;
    }
    
    self.FindPswView.securitycodeTF.text=@"";
    btn.userInteractionEnabled = NO;
    self.timeCount = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reduceTime:) userInfo:btn repeats:YES];
    
    
    
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    
    [parametersDic setObject:self.FindPswView.phoneTF.text forKey:@"phone"];
    
    [self doHttpRequestWithApi:API_SEND_SMS method:POST dic:parametersDic successBlock:^(NSDictionary *dic) {
        [self showMessage:MyString(@"验证码发送成功，请查收") ];
    }];
    
    
}

- (void)reduceTime:(NSTimer *)codeTimer {
    self.timeCount--;
    if (self.timeCount == 0) {
        [_FindPswView.getsecuritycodeBtn setTitle:MyString(@"重新获取") forState:UIControlStateNormal];
        [_FindPswView.getsecuritycodeBtn setTitleColor:[UIColor colorWithRed:205/255.0f green:8/255.0f blue:20/255.0f alpha:1] forState:UIControlStateNormal];
        UIButton *info = codeTimer.userInfo;
        info.enabled = YES;
        _FindPswView.getsecuritycodeBtn.userInteractionEnabled = YES;
        [self.timer invalidate];
    } else {
        _FindPswView.getsecuritycodeBtn.titleLabel.font = [UIFont systemFontOfSize: 13.0];
        NSString *str = [NSString stringWithFormat:@"%@(%lu)", MyString(@"重新获取"),self.timeCount];
        [_FindPswView.getsecuritycodeBtn setTitle:str forState:UIControlStateNormal];
        _FindPswView.getsecuritycodeBtn.userInteractionEnabled = NO;
        
    }
}


#pragma -----------键盘下降方法--------
-(BOOL) textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}
-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{
    [self.view endEditing:YES];
}


-(void)nextClick:(UIButton *)btn
{
    
    
    //键盘下降 结束编辑
    [self.view endEditing:YES];
    
    if (self.FindPswView.securitycodeTF.text.length==0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:MyString(@"请输入验证码") delegate:self cancelButtonTitle:nil otherButtonTitles:MyString(@"确定"), nil];
        [alert show];
        return;
    }
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    [parametersDic setObject:self.FindPswView.phoneTF.text forKey:@"phone"];
    [parametersDic setObject:self.FindPswView.securitycodeTF.text forKey:@"smsCode"];
    [self doHttpRequestWithApi:API_CHECK_SMS method:GET dic:parametersDic successBlock:^(NSDictionary *dic) {
        ZJFindPswSecondViewController *senconfindVC=[[ZJFindPswSecondViewController alloc]init];
        senconfindVC.curphoneStr=self.FindPswView.phoneTF.text;
        senconfindVC.smscodeStr=self.FindPswView.securitycodeTF.text;
        [self.navigationController pushViewController:senconfindVC animated:YES];
        
    }];
    
    
}








@end
